package phone.qyg.com.qyg.activity.baidu;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.baidu.location.MyOrientationListener;
import com.app.base.tool.BaseActivity;
import com.app.config.ApplicationConfig;
import com.app.utils.TransformController;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.activity.FindAddressActivity;
import phone.qyg.com.qyg.activity.FintnessLineMapActivity;
import phone.qyg.com.qyg.model.ActivityDetail;
import phone.qyg.com.qyg.model.MapItem;
import phone.qyg.com.qyg.model.VenueDetail;
import phone.qyg.com.qyg.model.baidu.TranInfo;

/**
 * 作者：lblbh on 2017/12/8 15:51
 * 邮箱：lanbuhan@163.com
 * 功能：TODO
 */
@ContentView(R.layout.map)
public class MapActivity extends BaseActivity {

    @ViewInject(R.id.bmapView)
    MapView bmapView;

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.one_rela)
    RelativeLayout one_rela;

    @ViewInject(R.id.two_rela)
    RelativeLayout two_rela;

    @ViewInject(R.id.three_rela)
    RelativeLayout three_rela;

    BaiduMap mBaiduMap;


    LocationClient mLocationClient;
    boolean isFirstIn = true;
    double mLatitude;
    double mLongtitude;
    // 自定义定位图标
    BitmapDescriptor mIconLocation;
    MyOrientationListener myOrientationListener;
    float mCurrentX;
    MyLocationConfiguration.LocationMode mLocationMode;

    //属性
    MapItem mapItem;

    String cureentAddress;

    String latitude;

    String longitude;

    String address;

    String title;

    @Override
    public void initSucessView() {
        Object obj = getTransModels();

        if(obj instanceof VenueDetail){
            VenueDetail venueDetail = (VenueDetail) obj;
            latitude = venueDetail.latitude;
            longitude = venueDetail.longitude;
            title = venueDetail.title;
            address = venueDetail.address;
            mapItem = new MapItem(latitude,longitude);
        }else if(obj instanceof ActivityDetail){
            ActivityDetail actDetail = (ActivityDetail) obj;
            latitude = actDetail.latitude;
            longitude = actDetail.longitude;
            title = actDetail.title;
            address = actDetail.address;
            mapItem = new MapItem(latitude,longitude);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        initBMapView();

        initLocation();
    }


    private void initBMapView() {
        mBaiduMap = bmapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(20.0f);
        mBaiduMap.setMapStatus(msu);
    }

    private void initLocation(){

        mLocationMode = MyLocationConfiguration.LocationMode.NORMAL;
        mLocationClient = new LocationClient(this);
        MyMAPLocationListener mLocationListener = new MyMAPLocationListener();
        mLocationClient.registerLocationListener(mLocationListener);

        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        option.setScanSpan(1000);
        mLocationClient.setLocOption(option);
//        // 初始化图标
//        View view = View.inflate(QXApplication.getContext(),R.layout.baidu_img,null);
//        ImageView img = (ImageView) view.findViewById(R.id.img);
//        img.setImageResource(R.drawable.find_map_icon);
//        mIconLocation = BitmapDescriptorFactory.fromView(view);

//        mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.find_map_icon);

        myOrientationListener = new MyOrientationListener(this);

        myOrientationListener
                .setOnOrientationListener(new MyOrientationListener.OnOrientationListener()
                {
                    @Override
                    public void onOrientationChanged(float x)
                    {
                        mCurrentX = x;
                    }
                });
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        // 开启定位
        mBaiduMap.setMyLocationEnabled(true);
        if (!mLocationClient.isStarted())
            mLocationClient.start();
        // 开启方向传感器
        myOrientationListener.start();
    }

    @Override
    public void onPause() {
        if(bmapView != null)
            bmapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        if(bmapView != null)
            bmapView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        if (mLocationClient != null)
            mLocationClient.stop();
        if(bmapView != null)
            bmapView.onDestroy();
        super.onDestroy();
    }

    private class MyMAPLocationListener implements BDLocationListener
    {
        @Override
        public void onReceiveLocation(final BDLocation location)
        {

            MyLocationData data = new MyLocationData.Builder()//
                    .direction(mCurrentX)//
                    .accuracy(location.getRadius())//
                    .latitude(location.getLatitude())//
                    .longitude(location.getLongitude())//
                    .build();
            mBaiduMap.setMyLocationData(data);
            // 设置自定义图标
            MyLocationConfiguration config = new MyLocationConfiguration(
                    mLocationMode, true, null);
            mBaiduMap.setMyLocationConfigeration(config);

            // 更新经纬度
            mLatitude = location.getLatitude();
            mLongtitude = location.getLongitude();

            if (isFirstIn){
                LatLng latLng = new LatLng(isRequestStr(latitude)?Double.valueOf(latitude):mLatitude,
                                            isRequestStr(longitude)?Double.valueOf(longitude): mLongtitude);

                mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.find_map_icon);
                OverlayOptions options = new MarkerOptions().position(latLng).icon(mIconLocation)
                        .zIndex(5);
                Marker marker = (Marker) mBaiduMap.addOverlay(options);

                MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
                mBaiduMap.animateMapStatus(msu);

                isFirstIn = false;

                cureentAddress = location.getAddrStr();
            }

        }

        public void onConnectHotSpotMessage(String s, int i) {

        }

    }


    @Event(value = {R.id.back,R.id.one_rela,R.id.two_rela,R.id.three_rela})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.one_rela:

                LatLng oneLatLng = new LatLng(mLatitude, mLongtitude);
                if(oneLatLng == null) return;
                TransformController.transformControllerModel(QXApplication.getContext(),
                        FintnessLineMapActivity.class,
                        new TranInfo(Double.valueOf(latitude),
                                Double.valueOf(longitude),
                                oneLatLng.latitude,
                                oneLatLng.longitude,
                                address,
                                cureentAddress,
                                0));
                break;
            case R.id.two_rela:

                LatLng twoLatLng = new LatLng(mLatitude, mLongtitude);
                if(twoLatLng == null) return;
                TransformController.transformControllerModel(QXApplication.getContext(),
                        FintnessLineMapActivity.class,
                        new TranInfo(Double.valueOf(latitude),
                                Double.valueOf(longitude),
                                twoLatLng.latitude,
                                twoLatLng.longitude,
                                address,
                                cureentAddress,
                                1));
                break;
            case R.id.three_rela:
                Bundle bundle = new Bundle();
                bundle.putInt(TransformController.ChangeKEY.BUNDLE_KEY, ApplicationConfig.PERIPHERY_TYPE);
                bundle.putInt(TransformController.ChangeKEY.FIT_MAP_REQUEST,ApplicationConfig.FIT_MAP_TYPE);
                bundle.putParcelable(TransformController.ChangeKEY.FITNESSMAP,mapItem);
                TransformController.transformControllerIntPut(QXApplication.getContext(),
                        FindAddressActivity.class,
                        bundle);
                break;
        }
    }

    @Override
    public void initViewHandler() {

    }
}
